/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.async;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;

import com.phonebooksharing.android.util.GalleryUtilities;
import com.phonebooksharing.android.util.NetworkHandlerTask;

public class LoadSdCardImageTask extends NetworkHandlerTask<Object, Object, Void> {
    private ListView lv;
    private Context cx;

    public LoadSdCardImageTask(ListView lv) {
        this.lv = lv;
        this.cx = lv.getContext();
    }

    @Override
    public Void doInBackground(Object... params) {
        String file = (String)params[1];
        int  w  = (Integer)params[2];
        int  h  = (Integer)params[3];

        Bitmap b = GalleryUtilities.getBitmapFromFile(cx.getContentResolver(), new File(file), w, h);
//        final Bitmap bm = GalleryUtilities.scaleAndFrame(b, w, h);
//
//        b.recycle();
//        b = null;
        
        publishProgress(params[0], b, params[4]);
        return null;
    }
    
    @Override
    public void onProgressUpdate(Object... result) {
        super.onProgressUpdate(result);
        
        //common params
        final Long id = (Long)result[0];
        final Bitmap bitmap = (Bitmap)result[1];
        final View view;

        // skip if bitmap wasnt found
        if(bitmap == null) return;

        GalleryUtilities.addToImageCache(id, bitmap);

        final int lvPosition, groupPosition, childPosition;
        //normal listview
        if(lv instanceof ExpandableListView) {

            groupPosition = (Integer)result[2];
            childPosition = (Integer)result[3];
            
            final long packetPos = ExpandableListView.getPackedPositionForChild(groupPosition, childPosition);
            final ExpandableListView elv = (ExpandableListView)lv;
            
            //skip updating this item if outside of bounds
            if(elv.getFlatListPosition(packetPos) <  lv.getFirstVisiblePosition() || elv.getFlatListPosition(packetPos) > lv.getLastVisiblePosition()) return;
            
            // find actual position and update view
            int visible = elv.getFlatListPosition(packetPos) - lv.getFirstVisiblePosition();
            view = lv.getChildAt(visible + 2);//add 2 (list view header and group header)
            
        } else {
            lvPosition = (Integer)result[2];
          //skip updating this item if outside of bounds
            if(lvPosition < lv.getFirstVisiblePosition() || lvPosition > lv.getLastVisiblePosition()) return;
            
            // find actual position and update view
            int visible = lvPosition - lv.getFirstVisiblePosition();
            view = lv.getChildAt(visible);
        }
        
        final ImageView iv;
        iv = (ImageView)view.findViewById(android.R.id.icon);
        
        iv.removeCallbacks(null);
        iv.setImageBitmap(null);
        iv.setImageBitmap(bitmap);
    }
}